
***		

/*******************************************************************************
	Graph Setup
*******************************************************************************/

	* Manually override Stata scheme
	set scheme s2color
	global graph_style "graphregion(color(white))"
	
	* Manually introduce Okabe-Ito's colorblind-friendly palette
	global black = "0 0 0"
	global orange = "230 159 0"
	global skyblue = "86 180 233"
	global green = "0 158 115"
	global yellow = "240 228 66"
	global blue = "0 114 178"
	global vermillion = "213 94 0"
	global purple = "204 121 167"
	
	
/*******************************************************************************
	Notes:
	
	At the time of graph construction, RBI's DBIE website experienced outage
	and as such, we decided to query the data manually from RBI Bulletin, which
	is released at monthly basis and contains biweekly data. However, the file
	that contains data for monetary base (M0) is separate from the one that 
	contains other observations (currency circulation, M1 to M4). As the 
	result, we downloaded monthly bulletin data for Table 6 (Money Stock Measures)
	and Table 10 (RBI Survey), and the code for data cleaning process below 
	reflects this practice
	
*******************************************************************************/
	
	
/*******************************************************************************
		Importing and Cleaning RBI's Table 6 Data (Money Stock Measures)
*******************************************************************************/

	local month "01 02 03 04 05 06 07 08 09 10 11 12"
	local year "2013 2014 2015 2016 2017 2018 2019"

	/* Import Excel, Create Intermediate Output as CSV */	
	foreach mo of local month{
		foreach yr of local year{
			import excel "$datadir/money/Money Supply/Money_`yr'_`mo'", clear
			missings dropobs, force
			missings dropvars, force
			drop in 6
			drop in 1/4
			replace B = "Item" if _n==1 & missing(B)
			drop C D E
			drop if _n>=17
			export delim "$datadir/money/Money Supply/csv/Money_`yr'_`mo'.csv", replace novar
		
		}
	}

	/* Import Intermediate CSV Output for Cleaning */
	foreach mo of local month{
		foreach yr of local year{
			import delim "$datadir/money/Money Supply/csv/Money_`yr'_`mo'.csv", clear
			display "`yr'_`mo'"
			qui ds item, not
			rename (`r(varlist)') value=
			reshape long value, i(item) j(date) string
			
			gen mo = substr(date,1,3)
			gen dt = substr(date,4,.)
			replace dt = "0"+dt if strlen(dt)==1
			replace date = mo+dt
			drop mo dt
			
			gen archive = "`yr'_`mo'"

			replace date = date + "`yr'"
			gen date_num = date(date,"MDY")
			format date_num %td	
			tempfile money_`yr'_`mo'
			save `money_`yr'_`mo''
		}
	}
	
	/* Append data from each monthly files */
	clear
	foreach mo of local month{
		foreach yr of local year{
			append using `money_`yr'_`mo''
		}
	}
	
	/* Change Crore to Billion */
	replace value = value*(10^7/10^9) if date_num > td(01jul2019)	
	
	
	/* Change to Blah */
	ren item raw_desc
	gen item = ""
	replace item = "Circ" if strpos(raw_desc, "Currency with the Public")
	replace item = "M1" if strpos(raw_desc, "1 + 2")
	replace item = "M2" if strpos(raw_desc, "3 + 4")
	replace item = "M3" if strpos(raw_desc, "3 + 6")
	keep if !mi(item)
	
	tempfile money_dataset
	save `money_dataset'
	
/*******************************************************************************
		Importing and Cleaning RBI's Table 10 Data (RBI Survey)
*******************************************************************************/

	
	/* Import Excel, Create Intermediate Output as CSV */	
	foreach mo of local month{
		foreach yr of local year{
			import excel "$datadir/money/Reserve Money//Reserve_`yr'_`mo'", clear 
			*missings dropobs, force
			missings dropvars, force
			scalar rawobs = _N
			drop in 6/7 if rawobs == 34
			drop in 7/8 if rawobs == 35
			drop in 1/4 if rawobs == 34
			drop in 1/5 if rawobs == 35
			replace B = "Item" if _n==1 & missing(B)
			drop C D E
			replace F = subinstr(F,"–","",.)
			replace G = subinstr(G,"–","",.)
			replace F = subinstr(F,"-","",.)
			replace G = subinstr(G,"-","",.)
			export delim "$datadir/money/Reserve Money/csv/Reserve_`yr'_`mo'.csv", replace novar
		
		}
	}
	
	/* Import Intermediate CSV Output for Cleaning */
	foreach mo of local month{
		foreach yr of local year{
			import delim "$datadir/money/Reserve Money/csv/Reserve_`yr'_`mo'.csv", clear encoding("utf-8")
			keep if strpos(item, "Reserve Money") > 0
			qui ds item, not
			rename (`r(varlist)') value=
			reshape long value, i(item) j(date) string
			
			gen mo = substr(date,1,3)
			gen dt = substr(date,4,.)
			replace dt = "0"+dt if strlen(dt)==1
			replace date = mo+dt
			drop mo dt
			
			gen archive = "`yr'_`mo'"
			
			replace date = date + "`yr'"
			gen date_num = date(date,"MDY")
			format date_num %td	
			tempfile res_`yr'_`mo'
			save `res_`yr'_`mo''
		}
	}
	
	clear
	foreach mo of local month{
		foreach yr of local year{
			append using `res_`yr'_`mo''
		}
	}
	
	/* Change Crore to Billion */
	replace value = value*(10^7/10^9) if date_num > td(01jul2019)
	ren item raw_desc
	gen item = ""
	replace item = "M0"
	
	tempfile res_dataset
	save `res_dataset'	

/*******************************************************************************
		Append and Save Consolidated Datasets
*******************************************************************************/	

	clear
	use `money_dataset'
	append using `res_dataset'
	
	save "$datadir/money/data_to_analyze", replace

	
/*******************************************************************************
		Replicating Graphs a la Lahiri (2020)
*******************************************************************************/	
		
	use "$datadir/money/data_to_analyze", clear

	/* Keep Necessary Variables Only */
	keep item value date_num archive
	reshape wide value, i(date_num) j(item) string
	ren value* *

	/* Generate Money in Circulation as Ratio of Money Supply Measure */	
	gen M0_ratio = Circ/M0
	gen M1_ratio = Circ/M1
	gen M2_ratio = Circ/M2
	gen M3_ratio = Circ/M3
	
	
	forval x = 0/3{
		replace M`x'_ratio = M`x'_ratio*100
	}

	la var M0_ratio "Currency in Circulation as % of M0"
	la var M1_ratio "Currency in Circulation as % of M1"
	la var M2_ratio "Currency in Circulation as % of M2"
	la var M3_ratio "Currency in Circulation as % of M3"
	

	/* Create Macros for Table Setting */	
	forval y = 2013/2019 {
		local jan `jan' `=mdy(1,1,`y')'
		local jul `jul' `=mdy(7,1,`y')'		
	}	
		
	scalar startdt = td(01jul2013)
	scalar enddt = td(31jul2019)
	scalar demonetization = td(08nov2016)
	scalar demonetization_qtr = (qofd(td(08nov2016))-1)+(td(08nov2016)-td(01oct2016))/91.25

	/* 4. Reinterpretation of Table 1: Money in Circulation as % of M3 */	
	#delimit ; 
	twoway (line M3_ratio date_num, lc("$orange") lw(medthick)), 
	$graph_style xlab(`jul', format(%tdCY) noticks labgap(4))  xtitle("")
	xline(`=scalar(demonetization)', lc(black) lp(dash)) 
	xtick(`jan',tlength(*0.75) grid) 
	xmlab(`=scalar(demonetization)' "{it:Demonetization}", notick labsize(small)) 
	ytitle("Cash in Circulation as % of Money Supply")
	legend(on region(lwidth(none)));
	graph export "$outputdir_fig/Figure_H_12.png", replace width(1500) height(1000);
	
	